var UsrNotice = {
    getCacheKey: function() {
        return $.cookie('NS') + 'UsrNotice'
    },
    init: function(param) {
        if (Storage.isSupported()) {
            var nsData = Storage.get(UsrNotice.getCacheKey());
            if (!nsData) {
                UsrNotice.request(param);
            } else {
                UsrNotice.populate(nsData);
            }
        } else {
            DynUsr.request();
        }
    },
    request: function(send) {
       var data='';
	   UsrNotice.populate(data);
    },
    populate: function(data) {
        Storage.set(UsrNotice.getCacheKey(), data);
        var big_html = '';
        current_sent_timezone = 1;
        if (handleState(data)) {
            if (data[42][0] == 0) {
                $('#menu_notice .unread').html(0);
            } else {
                $('#menu_notice .unread').html(data[42][0]);
                var html = '';
                var j = 0;
                $.each(data[42][1],
                function(i, val) {
                    if (i > current_unread_last) current_unread_last = i;
                    if ($('.menu_notice_item_' + i).length > 0) {} else {
                        current_unread_notices++;
                        j++;
                        var notice_text = '';
                        var notice_big_text = '';
                        if (val[0] == 51) {
                            var tmp = '';
                            var status_title = '';
                            if (data[42][3][val[2]]) {
                                if (! (val[3] && data[42][2][val[3]])) {
                                    tmp += getTranslation('Administration') + ' <br/>';
                                    status_title += getTranslation('Administration') + ' <br/>';
                                }
                                tmp += val[1] + ' ';
                                if (data[42][3][val[2]][0] != data[42][3][val[2]][1]) {
                                    tmp += getTranslation('notice_' + val[0]) + ' ' + getTranslation('MMB_STATUS_' + data[42][3][val[2]][1]);
                                    status_title += getTranslation('notice_' + val[0]) + ' ' + getTranslation('MMB_STATUS_' + data[42][3][val[2]][1]);
                                    if (data[42][3][val[2]][2]) tmp += '<br/>';
                                }
                                if (data[42][3][val[2]][2]) {
                                    tmp += data[42][3][val[2]][2];
                                }
                            }
                            if (tmp.length > 11) {
                                notice_text += tmp;
                            } else {
                                notice_text += 'notice_status_not_found<br/>id_' + val[2] + '<br/>uid_' + val[3];
                            }
                            if (val[4]['msg_for_user']) {
                                notice_big_text += '<div class="notice_date">' + val[1] + '</div>';
                                notice_big_text += '<div class="notice_status">' + status_title + '</div>';
                                notice_big_text += '<div class="notice_text">' + val[4]['msg_for_user'] + '</div>';
                            }
                        } else {
                            notice_text += val[1] + ' ';
                            var tmp = '';
                            if (val[0] != 999) {
                                tmp += getTranslation('notice_' + val[0]);
                            }
                            if (val[2]) tmp += ' ' + getTranslation(val[2]);
                            if (tmp == ' ') {} else {
                                notice_text += tmp;
                            }
                        }
                        if (notice_text && val[3] && data[42][2][val[3]] && data[42][2][val[3]][0]) {
                            notice_text = data[42][2][val[3]][0] + ' <br/>' + notice_text;
                        }
                        if (notice_big_text) {
                            big_html += '<div class="arrg arrg_in menu_notice_item_' + i + '" onmouseover="$(this).addClass(\'notice-active\');" ';
                            big_html += ' onmouseout="$(this).removeClass(\'notice-active\');"';
                            big_html += '><div>';
							big_html += '<a href="#" class="easyui-menubutton" onclick="readNotice(' + i + '); return false;" style="float:right;">[x]</a>';
							big_html += notice_big_text;
                            big_html += '</div></div>';
                        }
                        if (notice_text) {
                            html += '<div class="menu_notice_item_' + i + '" onmouseover="$(this).addClass(\'notice-active\');" ';
                            html += ' onmouseout="$(this).removeClass(\'notice-active\');"';
                            if (j == data[42][0]) html += ' class="menu_notice_item last"';
                            else html += ' class="menu_notice_item inner"';
                            html += '><div>';
                            html += '<a href="#" class="easyui-menubutton" onclick="readNotice(' + i + '); return false;" style="float:right;">[x]</a>';
							html += notice_text;
                            html += '</div></div>';
                        }
                    }
                });
                $('#noticesSub').append(html).show();
                var max_h = 10;
                $.each($('.menu_notice_item'),
                function(i, val) {
                    var h = $(val).height() + '';
                    max_h += 1 * parseInt(h.replace('px', ''), 10) + 4;
                });
                $('#noticesSub').hide();
                var pos = $('#menu_notice').position();
                $('#noticesSub').css({
                    'height': max_h + 'px',
                    'overflow-y': 'scroll',
                    'zIndex': 10050000,
                    'top': pos.top + 30,
                    'left': pos.left
                });
            }
            setTimeout(checkNotice, 300000);
        } else {
            $('#menu_notice .unread').html(0);
        }
        if (big_html) {
            $('#desktop_notices').html(big_html);
        } else {
            $('#desktop_notices').html('');
        }
    }
};